package com.wenwei.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.amap.api.maps.model.Text;
import com.jakewharton.rxbinding.widget.RxTextView;
import com.orhanobut.logger.Logger;
import com.wenwei.net.ApiManager;
import com.wenwei.net.BaseSubscriber;
import com.wenwei.parking.BaseActivity;
import com.wenwei.parking.R;
import com.wenwei.utils.UserStringUtils;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func6;

/**
 * Created by Administrator on 2017/8/22.
 */
public class InvoiceActivity extends BaseActivity {
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.user_msg_et)
    com.wenwei.view.ContainsEmojiEditText userMsgEt;
    @Bind(R.id.user_price_et)
    com.wenwei.view.ContainsEmojiEditText userPriceEt;
    @Bind(R.id.user_remark_et)
    com.wenwei.view.ContainsEmojiEditText userRemarkEt;
    @Bind(R.id.user_name_et)
    com.wenwei.view.ContainsEmojiEditText userNameEt;
    @Bind(R.id.user_phone_et)
    com.wenwei.view.ContainsEmojiEditText userPhoneEt;
    @Bind(R.id.user_address_et)
    com.wenwei.view.ContainsEmojiEditText userAddressEt;
    @Bind(R.id.bind_btn)
    TextView bindBtn;
    @Bind(R.id.xx_price)
    TextView priceTv;


    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_invoice;
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("发票");
        Observable<CharSequence> msgObservable = RxTextView.textChanges(userMsgEt);
        Observable<CharSequence> priceObservable = RxTextView.textChanges(userPriceEt);
        Observable<CharSequence> remarkObservable = RxTextView.textChanges(userRemarkEt);
        Observable<CharSequence> nameObservable = RxTextView.textChanges(userNameEt);
        Observable<CharSequence> phoneObservable = RxTextView.textChanges(userPhoneEt);
        Observable<CharSequence> addressObservable = RxTextView.textChanges(userAddressEt);
        Observable.combineLatest(msgObservable, priceObservable, remarkObservable, nameObservable, phoneObservable,
                addressObservable, new Func6<CharSequence, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence, Boolean>() {
            @Override
            public Boolean call(CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3, CharSequence charSequence4, CharSequence charSequence5, CharSequence charSequence6) {
                return !charSequence.toString().equals("") && !charSequence2.toString().equals("")&& !charSequence3.toString().equals("")
                        && !charSequence4.toString().equals("")&& !charSequence5.toString().equals("")&& !charSequence6.toString().equals("");
            }
        }).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                bindBtn.setEnabled(aBoolean);
                if(aBoolean){
                    bindBtn.setBackgroundResource(R.drawable.button_state_2);
                }else{
                    bindBtn.setBackgroundResource(R.drawable.button_state_1);
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.base_back, R.id.bind_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finish();
                break;
            case R.id.bind_btn:
                addInvoice();
                break;
        }
    }

    private double mPrice;

    @Override
    protected void initData() {
        super.initData();
        getUsableInvoiceAmount();
    }

    //可开金额
    private BaseSubscriber<Double> priceSb;
    private void getUsableInvoiceAmount(){
        priceSb = new BaseSubscriber<Double>(mContext) {
            @Override
            public void onSuccess(Double integer) {
                mPrice = integer;
                priceTv.setText(mPrice+"");
                Logger.d("可开金额为："+mPrice);
            }
        };
        ApiManager.getInstance().getUsableInvoiceAmount(priceSb, UserStringUtils.getUserId(mContext));
    }

    //索要发票
    private BaseSubscriber<Object> addSb;
    private void addInvoice(){
        addSb = new BaseSubscriber<Object>(mContext,true,"请稍后...") {
            @Override
            public void onSuccess(Object o) {
                showToast("提交成功");
                finish();
            }
        };
        try{
//            {
//                "address": "收件地址,String",
//                    "addressee": "收件人,String",
//                    "amount": 1,
//                    "contactNumber": "18328676067",
//                    "remake": "备注说明,String",
//                    "userId": 1,
//                    "username": "发票人名或者是公司,String"
//            }
            JSONObject jb = new JSONObject();
            jb.put("address",userAddressEt.getText().toString());
            jb.put("addressee",userNameEt.getText().toString());
            jb.put("amount",Double.valueOf(userPriceEt.getText().toString()));
            jb.put("contactNumber",userPhoneEt.getText().toString());
            jb.put("remake",userRemarkEt.getText().toString());
            jb.put("userId",UserStringUtils.getUserId(mContext));
            jb.put("username",userMsgEt.getText().toString());
            ApiManager.getInstance().addInvoice(addSb,jb.toString());

        }catch (Exception e){}
    }
}
